﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Net.Http;
using Xinchen.Utils;
using System.Net;

namespace Xinchen.SOA.Server.ControllerResolver
{
    public class ControllerSelector : IHttpControllerSelector
    {
        HttpConfiguration _config;
        IDictionary<string, HttpControllerDescriptor> _desriptors = new Dictionary<string, HttpControllerDescriptor>(StringComparer.OrdinalIgnoreCase);
        private IEnumerable<Assembly> _assemblies;
        private bool _isManagerService = false;
        private string _managerKey;
        private int _managerId;
        public ControllerSelector(HttpConfiguration config)
        {
            _config = config;
            _managerKey = ConfigHelper.GetConfigAppSettingValue("applicationKey");
            _managerId = ConvertHelper.ToInt32(ConfigHelper.GetConfigAppSettingValue("applicationId"));
        }

        void InitControllers()
        {
            if (_desriptors.Count <= 0)
            {
                lock (_desriptors)
                {
                    if (_desriptors.Count <= 0)
                    {
                        _assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.GlobalAssemblyCache && !x.IsDynamic);
                        var controllerTypes = new List<Type>();
                        foreach (var ass in _assemblies)
                        {
                            controllerTypes.AddRange(ass.GetExportedTypes());
                        }
                        var descriptors = new Dictionary<string, HttpControllerDescriptor>();
                        foreach (var controllerType in controllerTypes)
                        {
                            if (controllerType.Namespace == "Xinchen.SOA.Manager.Api.Core.Services")
                            {
                                _isManagerService = true;
                            }
                            if (typeof(ApiController).IsAssignableFrom(controllerType))
                            {
                                var descriptor = new HttpControllerDescriptor(_config, controllerType.Name, controllerType);
                                _desriptors.Add(descriptor.ControllerName, descriptor);
                            }
                        }
                    }
                }
            }
        }

        public IDictionary<string, HttpControllerDescriptor> GetControllerMapping()
        {
            InitControllers();
            return _desriptors;
        }

        public System.Web.Http.Controllers.HttpControllerDescriptor SelectController(System.Net.Http.HttpRequestMessage request)
        {
            InitControllers();
            var routeData = request.GetRouteData();
            var controllerName = Convert.ToString(routeData.Values.Get("controller"));
            return _desriptors.Get(controllerName);
        }

    }
}
